package lava.rt.linq.sql;


import java.io.Serializable;


public class SelectCommand implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public final String columns, from,where, orderby;
	
	
	private Object[] params=new Object[0];
	
	
	private final String sql,countSql,deleteSql;
	
    
	
    
    public SelectCommand(String columns, String from,String where, String orderby) {
		super();
		this.columns = columns;
		this.from = from;
		this.where=where;
		this.orderby = orderby;
		
		StringBuffer ret=new StringBuffer();
		ret.append("select ")
		.append(columns)
		.append(" from ")
		.append(from);
		if(!isBlank(where)) {
		  ret.append(" where ").append(where);
		}
		if(!isBlank(orderby)) {
		 ret.append(" order by ").append(orderby);
					
		}
		
		this.sql=ret.toString();
		
		
		ret=new StringBuffer();
		ret.append("select ")
		.append(" count(*) ")
		.append(" from ")
		.append(from);
		if(!isBlank(where)) {
		  ret.append(" where ").append(where);
		}
		
		this.countSql=ret.toString();
		
		
		ret=new StringBuffer();
		ret.append("delete ")
		.append(" from ")
		.append(from);
		if(!isBlank(where)) {
		  ret.append(" where ").append(where);
		}
		
		this.deleteSql=ret.toString();
		
	}
	
	
	
	

	
	public void setParams(Object... params) {
		this.params = params;
	}






	public Object[] getParams() {
		return params;
	}




	
	private boolean isBlank(String value) {
		// TODO Auto-generated method stub
		return value==null||value.trim().length()<=0;
	}






	protected String getSql() {
		return this.sql;
	}
	
	
	


	public String getCountSql() {
		return countSql;
	}






	public String getDeleteSql() {
		return deleteSql;
	}






	public String getUpdateSql(String set) {
		StringBuffer ret=new StringBuffer();
		ret.append("update ")
		.append(from)
		.append(" set ")
		.append(set)
		;
		if(!isBlank(where)) {
		  ret.append(" where ").append(where);
		}
		return ret.toString();
	}
	
	
	
	
	
	
	
	
	
	
	
}
